package com.lm.xstream.example;

import com.lm.xstream.ele.Person;
import com.thoughtworks.xstream.XStream;

public class ConverterXmlExample {
    public static void main(String[] args) {
        XStream xstream = new XStream();

        // 安全设置：允许反序列化的类
        XStream.setupDefaultSecurity(xstream);
        xstream.allowTypes(new Class[] {Person.class});

        // 或者使用通配符，允许指定包下的类
        xstream.allowTypesByWildcard(new String[] {"com.lm.xstream.ele.**"});

        // XML 字符串
        String xml = "<person> <name>John</name> <age>30</age> </person> ";

        // XML 转对象
        xstream.alias("person", Person.class); // 设置别名
        Person person = (Person) xstream.fromXML(xml);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}
